﻿using System.Collections.Generic;
using System.Linq;

namespace Tuxedo.Compile
{
	internal class DoWhileLoop : CodeLine
	{
		private CodeLine[] loopCode;
		private Expression condition;

		public DoWhileLoop(Expression condition, IList<CodeLine> loopCode)
		{
			this.loopCode = loopCode.ToArray();
			this.condition = condition;
		}

		public static CodeLine Parse(TokenStream tokens)
		{
			tokens.PopExpected("do");
			IList<CodeLine> loopCode = CodeLine.ParseBlock(tokens, false);
			tokens.PopExpected("while");
			tokens.PopExpected("(");
			Expression condition = Expression.ParseExpression(tokens);
			tokens.PopExpected(")");
			tokens.PopExpected(";");

			return new DoWhileLoop(condition, loopCode);
		}
	}
}
